package com.zzuli.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzuli.practice.entity.Post;
import com.zzuli.practice.entity.Repairman;
import com.zzuli.practice.mapper.PostMapper;
import com.zzuli.practice.service.IPostService;
import com.zzuli.practice.util.SensitiveFilter;
import javafx.geometry.Pos;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

@Service
public class PostService implements IPostService {

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private PostMapper postMapper;

    public IPage<Post> pagePostList(Integer pageIndex){
        if (pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        // 首先需要定义一个拦截器才能使用
        Page<Post> page = new Page<>(pageIndex, 5);

        IPage<Post> iPage = postMapper.selectPage(page, null);
        // 判断当前页是否大于总页数
        if (iPage.getCurrent() > iPage.getPages()){
            page.setCurrent(iPage.getPages());
        }
        return postMapper.selectPage(page, null);
    }

    public int insertPost(Post post){

        // html标签转义
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));

        // 敏感词过滤
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));

        return postMapper.insert(post);
    }

    public Post selectPostById(Integer id){
        return postMapper.selectById(id);
    }

    public int updateHeaderImg(String username, String headerimg){
        UpdateWrapper<Post> wrapper = new UpdateWrapper<>();
        wrapper.eq("username", username);
        wrapper.set("headerimg", headerimg);
        return postMapper.update(null, wrapper);
    }

    public IPage<Post> searchPagePost(Integer pageIndex, String title, String username){
        if (pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        Page<Post> page = new Page<>(pageIndex, 5);
        QueryWrapper<Post> wrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(title)){
            wrapper.isNotNull("id");
        } else{
            wrapper.like("title",title);
        }
        if (!StringUtils.isBlank(username)){
            wrapper.and(wrapper1->wrapper1.like("username", username));
        }
        IPage<Post> iPage = postMapper.selectPage(page, wrapper);
        // 判断当前页是否大于总页数
        if (iPage.getCurrent() > iPage.getPages()){
            page.setCurrent(iPage.getPages());
        }
        return postMapper.selectPage(page, wrapper);

    }

    public int deletePost(Integer id){
        return postMapper.deleteById(id);
    }

}
